public class ctrl_AmendmentSelectionClass
{
    //For Variables ...
    public ApexPages.StandardController controllerProperty {get;set;}
    public Account accountObject {get;set;}
    
    //For Flags...
    Public Boolean locationKeyFlag {get;set;}
    Public Boolean accountActivityOperationFlag {get;set;}
    Public Boolean accountActivityFlag {get;set;}
    Public Boolean locationChangeFlag {get;set;}
    Public Boolean locationChangedFlag {get;set;}
    Public Boolean locationAddFlag{get;set;}
    Public Boolean locationRemoveFlag{get;set;}
    Public Boolean companyNameFlag {get;set;}
    Public Boolean companyManagerFlag {get;set;}
    Public Boolean shareHolderFlag {get;set;}
    Public Boolean licenseTypeFlag {get;set;}
    //Selected Opertation...
    Public string selectedRecordType {get;set;}
    Public string selectedLocationKey {get;set;}
    Public string selectedChangeLocationOperations {get;set;}
    Public string selectedLocationChange {get;set;}
    Public RecordType accountRecordType = null;
    
    //For List Select Radio Types...
    Public List<SelectOption> recordTypeNameOptions {get;set;}
    Public List<SelectOption> locationKeyOptions {get;set;}
    Public List<SelectOption> changeAccountActivityOptions {get;set;}
    Public List<SelectOption> locationChangeOptions {get;set;}
    Public List<String> tenancyContractLocationList {get;set;}
    Public List<Share_Ownership__c> shareOwnerShipList {get;set;}
    
    //for maps...
    Map<Id,RecordType> amendmentRecordTypeMap = new Map<ID,RecordType>();
    Map<Id,Tenancy_Contract__c> amendmentTenancyContractForKeyMap = new Map<Id,Tenancy_Contract__c>();
    List<Account_Activity__c> amendmentAccountActivityList = new List<Account_Activity__c>();
    
    Public List<wrapperClass> accountActivityItemList{get;set;}
    Public List<wrapperClass> removeLocationItemList{get;set;}
    
    //For Constructors...
    public ctrl_AmendmentSelectionClass(ApexPages.StandardController controller) 
    {
        
        //Assign the Controller to the Property...
        controllerProperty  = controller;
       
        //Flag Reset for Record Type...
        selectedRecordType = null;
        
        //For Location Key Record Type...
        selectedLocationKey = null;
        locationKeyFlag =false;
        
        //For Location Change Record Type...
        selectedLocationChange = null;
        locationChangeFlag = false;
        locationChangedFlag = false;
        
        //Account Activity Record Type...
        selectedChangeLocationOperations = null;
        accountActivityItemList = new List<wrapperClass>();
        accountActivityOperationFlag = false;
        accountActivityFlag = false;
        changeAccountActivityOptions = new List<SelectOption>();
        changeAccountActivityOptions.add(new SelectOption('Add','Add'));
        changeAccountActivityOptions.add(new SelectOption('Edit','Edit'));
        changeAccountActivityOptions.add(new SelectOption('Delete','Delete'));
        
        //Location Amendment - Remove Location...
        removeLocationItemList = new List<wrapperClass>();
        locationRemoveFlag = false;
        
        //Location Amendment - Add Location...
        locationAddFlag = false;
        tenancyContractLocationList = new List<string>();
        
        //Company Amendment - Change Company Name...
        companyNameFlag = false;
        
        //Company Amendment - Change Company Manager
        companyManagerFlag = false;
        
        //For Share Holder Information...
        shareHolderFlag = false;
        
        //For Amendment Activity INformation...
        licenseTypeFlag = false;
    }
    
    //Search Criteria Method Button...
    public PageReference searchAllAmendmentTemplateMethod()
    {
        try
        {
             //Selected Account Name...
            accountObject = new Account(); //(Account)controllerProperty.getRecord();
            accountObject = [select id,Name,Active_Account_Activities__c,Login_User_Profile__c,Dummy_Activity__c,License_Application_Type__c,LegalStatus__r.Short_Name_English__c,Authorized_Manager_Name__c,Authorized_Manager__c,company_name_arabic__c,Residence_Visa_Quota_Left_2__c, Amendment_Change_Location_3__c,Amendment_Change_Location_2__c,Amendment_Change_Location__c,RecordTypeId,License_Application_Step__c,Activity__c from Account where Id =: ApexPages.CurrentPage().getParameters().get('accountId')];
            
            //Change the Account Record Type TO Amendment...
            accountRecordType = new RecordType();
            accountRecordType = [select Id,SObjectType,IsActive,Name,DeveloperName from RecordType where IsActive =: true AND SObjectType =: 'Account' AND DeveloperName =: 'Under_Amendment'];
            
            
            //Initialize the Record Type Options...     
            recordTypeNameOptions = new List<SelectOption>();
            
            //Query for the Record types of Amendment Object...
            for(RecordType recordType : [Select Id,SObjectType,Name,DeveloperName from RecordType where SObjectType =: 'Amendment__c' AND IsActive =: true AND DeveloperName NOT IN ('Company_Amendment_Change_Company_Name_Read_Only','Shareholder_Amendment_Replace_Partner_ReadOnly','Shareholder_Amendment_Remove_Partner_ReadOnly')])
            {
                //amendmentRecordTypeItemList.add(new wrapperClass(recordType));
                if(accountObject.Login_User_Profile__c == 'SmartPaper BPM: 08 License - Front Office Payment User' || accountObject.Login_User_Profile__c == 'SmartPaper BPM: 01 Front Office User' || accountObject.Login_User_Profile__c == 'System Administrator')
                {
                	if(recordType.DeveloperName == 'Company_Amendment_Change_Company_Manager'|| recordType.DeveloperName == 'Location_Amendment_New_Key' || recordType.DeveloperName == 'Shareholder_Amendment_Add_Partner' || recordType.DeveloperName == 'Shareholder_Amendment_Change_Share_Percentage' || recordType.DeveloperName == 'Shareholder_Amendment_Remove_Partner' || recordType.DeveloperName == 'Shareholder_Amendment_Replace_Partner')
                	{
                		System.debug('The Record Type Id => '+ recordType.Id + ' && The Record Type Name => '+ recordType.name);
		                recordTypeNameOptions.add(new SelectOption(recordType.Id,recordType.name));
		                amendmentRecordTypeMap.put(recordType.Id,recordType);	
                		
                	}
                		
                	
                }
                
                if(accountObject.Login_User_Profile__c == 'SmartPaper BPM: 09 License - Marketing Staff' || accountObject.Login_User_Profile__c == 'System Administrator')
                {
                	if(recordType.DeveloperName == 'Company_Amendment_Change_Company_Activity'|| recordType.DeveloperName == 'Company_Amendment_Change_Company_Name' || recordType.DeveloperName == 'Company_Amendment_Change_License_Type' || recordType.DeveloperName == 'Location_Amendment_Add_Location' || recordType.DeveloperName == 'Location_Amendment_Change_Location' || recordType.DeveloperName == 'Location_Amendment_Remove_Location')
                	{
                		System.debug('The Record Type Id => '+ recordType.Id + ' && The Record Type Name => '+ recordType.name);
		                recordTypeNameOptions.add(new SelectOption(recordType.Id,recordType.name));
		                amendmentRecordTypeMap.put(recordType.Id,recordType);	
                		
                	}
                	
                }
                
                   
            }
            
            if(recordTypeNameOptions.size() > 0)
            {
                //For Location Keys....
                locationKeyOptions = new List<SelectOption>();
                
                //For Location Change...
                locationChangeOptions = new List<SelectOption>();
                
                //For Share Ownership List...
                shareOwnerShipList = new List<Share_Ownership__c>();
                //For Query for the Location Associated to the ActiveTenancyContract..
                for(Tenancy_Contract__c tenancyObject : [Select Tenant__c, Tenancy_Contract_Expiry_Date__c, RecordTypeId, Name,Location__r.Visa_Quota__c,Location__r.Number_of_Keys__c, Location__r.Name, Location__r.Id, Location__c, Id From Tenancy_Contract__c WHERE Tenancy_Contract_Expiry_Date__c >: System.today() AND Tenant__c =: ApexPages.CurrentPage().getParameters().get('accountId') AND Tenancy_Contract_Cancellation_Date__c = null])
                {
                    locationKeyOptions.add(new SelectOption(tenancyObject.Id,tenancyObject.Location__r.Name));
                    locationChangeOptions.add(new SelectOption(tenancyObject.Id,tenancyObject.Location__r.Name));
                    amendmentTenancyContractForKeyMap.put(tenancyObject.Id,tenancyObject);
                    tenancyContractLocationList.add(tenancyObject.Location__r.Name);
                    removeLocationItemList.add(new wrapperClass(tenancyObject));
                    
                }
                
                //For Account Activity Logic...
                for(Account_Activity__c accountActivityObject : [SELECT Id,Active__c, Account__c, Activity_English_Name__c,Activity__c from Account_Activity__c where Account__c =: ApexPages.CurrentPage().getParameters().get('accountId') AND Active__c = : true])
                {
                    System.debug('The Account Activity English Name => '+ accountActivityObject.Activity_English_Name__c);
                    accountActivityItemList.add(new wrapperClass(accountActivityObject));  
                    amendmentAccountActivityList.add(accountActivityObject);                                     
                    
                } 
                
                //For Shareholder Information...
                //for(Share_Ownership__c shareOwnerObject : [SELECT Id,Ownership__c,Shareholder_English_Name__c,Shareholder__c,Share_Ownership_Type__c,Share_Profit__c,Company__c from Share_Ownership__c where Company__c =:ApexPages.CurrentPage().getParameters().get('accountId') AND Share_Ownership_Type__c Like '%Shareholder%'])
                for(Share_Ownership__c shareOwnerObject : [SELECT Id,Ownership__c,Shareholder_English_Name__c,Shareholder__c,Share_Ownership_Type__c,Share_Profit__c,Company__c from Share_Ownership__c where Company__c =:ApexPages.CurrentPage().getParameters().get('accountId')])
                {
                    //Adding in the list...
                    shareOwnerShipList.add(shareOwnerObject);
                
                }
                
            }
            //Return the Value...
            return null;    
            
        }
        catch(Exception exp)
        {
            
            return null;
        }
        
    } 
    
    //For Update the Flag...
    Public PageReference updateFlagsForRerenderMethod()
    {
        System.debug('The Selected Flag Id =>'+ selectedRecordType + ' && the Selected Record Type Name => ' + amendmentRecordTypeMap.get(selectedRecordType).Name );
        try
        {
        	if(amendmentRecordTypeMap.get(selectedRecordType).Name == 'Location Amendment - New Key')
	        {
	            locationKeyFlag = true;
	            
	            accountActivityOperationFlag = false;
	            accountActivityFlag = false;
	            
	            locationChangeFlag = false;
	            locationChangedFlag = false;
	            locationAddFlag = false;
	            locationRemoveFlag = false;
	            companyNameFlag = false;
	            companyManagerFlag = false;
	            shareHolderFlag = false;
	            licenseTypeFlag = false;
	        }
	        else if(amendmentRecordTypeMap.get(selectedRecordType).Name == 'Location Amendment - Change Location')
	        {
	            locationChangeFlag = true;
	            locationKeyFlag = false;
	            accountActivityOperationFlag = false;
	            locationAddFlag = false;
	            accountActivityFlag = false;
	            locationRemoveFlag = false;
	            companyNameFlag = false;
	            companyManagerFlag = false;
	            shareHolderFlag = false;
	            licenseTypeFlag = false;
	            System.debug('The Selected Location Change Value => '+ selectedLocationChange);
	            if(selectedLocationChange == '' || selectedLocationChange == null)
	                locationChangedFlag = false;
	            else
	                locationChangedFlag = true; 
	                
	            System.debug('The Selected Location ChangeD Flag => '+ locationChangedFlag);
	        }
	        else if(amendmentRecordTypeMap.get(selectedRecordType).Name  == 'Company Amendment - Change Company Activity')
	        {
	            accountActivityOperationFlag = true;
	            locationKeyFlag = false;
	            locationChangeFlag = false;
	            locationChangedFlag = false;
	            locationAddFlag = false;
	            locationRemoveFlag = false;
	            companyNameFlag = false;
	            companyManagerFlag = false;
	            shareHolderFlag = false;
	            licenseTypeFlag = false;
	            if(selectedChangeLocationOperations == 'Edit' || selectedChangeLocationOperations == 'Delete')
	                accountActivityFlag = true;
	            else
	                accountActivityFlag = false;    
	            
	            
	        }
	        else if(amendmentRecordTypeMap.get(selectedRecordType).Name  == 'Location Amendment - Add Location')
	        {
	            locationAddFlag = true;
	            accountActivityOperationFlag = false;
	            locationKeyFlag = false;
	            locationChangeFlag = false;
	            locationChangedFlag = false;
	            accountActivityFlag = false;
	            locationRemoveFlag = false;    
	            companyNameFlag = false;
	            companyManagerFlag = false;
	            shareHolderFlag = false;
	            licenseTypeFlag = false;
	            
	            
	        }
	        else if(amendmentRecordTypeMap.get(selectedRecordType).Name  == 'Location Amendment - Remove Location')
	        {
	            locationRemoveFlag = true;  
	            locationAddFlag = false;
	            accountActivityOperationFlag = false;
	            locationKeyFlag = false;
	            locationChangeFlag = false;
	            locationChangedFlag = false;
	            accountActivityFlag = false;
	            companyNameFlag = false;
	            companyManagerFlag = false;
	            shareHolderFlag = false;
	            licenseTypeFlag = false;
	             
	        }
	        else if(amendmentRecordTypeMap.get(selectedRecordType).Name  == 'Company Amendment - Change Company Name')
	        {
	            companyNameFlag = true;
	            locationRemoveFlag = false;  
	            locationAddFlag = false;
	            accountActivityOperationFlag = false;
	            locationKeyFlag = false;
	            locationChangeFlag = false;
	            locationChangedFlag = false;
	            accountActivityFlag = false;
	            companyManagerFlag = false;
	            shareHolderFlag = false;
	            licenseTypeFlag = false;
	             
	        }
	        //
	        else if(amendmentRecordTypeMap.get(selectedRecordType).Name  == 'Company Amendment - Change Company Manager')
	        {
	            companyManagerFlag = true;
	            locationKeyFlag = false;
	            accountActivityOperationFlag = false;
	            accountActivityFlag = false;
	            locationChangeFlag = false;
	            locationChangedFlag = false;
	            locationAddFlag = false;
	            locationRemoveFlag = false;
	            companyNameFlag = false;
	            shareHolderFlag = false;
	            licenseTypeFlag = false;
	            
	        }
	        else if(amendmentRecordTypeMap.get(selectedRecordType).Name  == 'Company Amendment - Change License Type')
	        {
	            licenseTypeFlag = true;
	            companyManagerFlag = false;
	            locationKeyFlag = false;
	            accountActivityOperationFlag = false;
	            accountActivityFlag = false;
	            locationChangeFlag = false;
	            locationChangedFlag = false;
	            locationAddFlag = false;
	            locationRemoveFlag = false;
	            companyNameFlag = false;
	            shareHolderFlag = false;
	            
	        }
	        else if(amendmentRecordTypeMap.get(selectedRecordType).Name  == 'Shareholder Amendment - Add Partner' || amendmentRecordTypeMap.get(selectedRecordType).Name == 'Shareholder Amendment - Change Share Percentage' || amendmentRecordTypeMap.get(selectedRecordType).Name == 'Shareholder Amendment - Remove Partner' || amendmentRecordTypeMap.get(selectedRecordType).Name == 'Shareholder Amendment - Replace Partner')
	        {
	            shareHolderFlag = true;
	            companyManagerFlag = false;
	            locationKeyFlag = false;
	            accountActivityOperationFlag = false;
	            accountActivityFlag = false;
	            locationChangeFlag = false;
	            locationChangedFlag = false;
	            locationAddFlag = false;
	            locationRemoveFlag = false;
	            companyNameFlag = false;
	            licenseTypeFlag = false;
	            
	            
	        }
	        else
	        {
	            //Reset all the flags...
	            locationKeyFlag = false;
	            accountActivityOperationFlag = false;
	            accountActivityFlag = false;
	            locationChangeFlag = false;
	            locationChangedFlag = false;
	            locationAddFlag = false;
	            locationRemoveFlag = false;
	            companyNameFlag = false;
	            companyManagerFlag = false;
	            shareHolderFlag = false;
	            licenseTypeFlag = false;
	        }
	        
	        System.debug('The Location Key Flag => '+ locationKeyFlag);
	        System.debug('The Account Activity Flag => '+ accountActivityFlag);
        	
        	
        }
        catch(Exception exp)
        {
        	ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, 'Please Select the Amendemnt Selections on the Page');
	        ApexPages.addMessage(msg);
	        return null;
        	
        }
        
        
        //Return the values...
        return null;
    }
    
    //For Save Button Method...
    public PageReference saveButtonMethod()
    {
        try
        {
            if(selectedRecordType != null)
            {
                    
                    //Company Amendment - Change License Type Business Logic...
                    if(amendmentRecordTypeMap.get(selectedRecordType).Name == 'Company Amendment - Change License Type')
                    {
                        if(amendmentTenancyContractForKeyMap.size() >= 0 )
                        {
                            Account selectedAccountObject =(Account)controllerProperty.getRecord();
                            System.debug('The Selected Account Object Dummy Activity => '+ selectedAccountObject.Dummy_Activity__c);
                            
                            if(selectedAccountObject.Dummy_Activity__c != null )
                            {
                                //Validation for the General Company...
                                /*if(!(accountObject.Name.contains('General Trading'))  && (selectedAccountObject.Dummy_Activity__c == 'Trading - General Trading' ))
                                {
                                    ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, 'Please Perform change company name before initiating this process.');
                                    ApexPages.addMessage(msg);
                                    return null;
                                }
                                
                                else*/ if(accountObject.Activity__c == selectedAccountObject.Dummy_Activity__c)
                                {
                                    ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, 'You have selected the existing company License Type. Please Select another License type to initiate the process.');
                                    ApexPages.addMessage(msg);
                                    return null;
                                }
                                
                                // Now Update the Account...
                                accountObject.recordtypeId = accountRecordType.id;
                                accountObject.License_Application_Type__c = 'Amendment';
                                accountObject.License_Application_Step__c = 'Change License Type Amendment Started';
                                update accountObject;
                                
                                System.debug('After Updating the Account');
                                
                                //For Creating the Amendment Record...
                                Amendment__c amendmentObject = new Amendment__c();
                                amendmentObject.recordTypeId = amendmentRecordTypeMap.get(selectedRecordType).id;
                                amendmentObject.Amendment_Application_Status__c = 'Change License Type Amendment Initiated';
                                amendmentObject.Related_To__c = 'Company';
                                amendmentObject.Account__c = ApexPages.CurrentPage().getParameters().get('accountId');
                                amendmentObject.Previous_License_Type__c = accountObject.Activity__c; 
                                amendmentObject.New_License_Type__c = selectedAccountObject.Dummy_Activity__c;
                                //Now Insert the new Record...
                                insert amendmentObject;
                                
                                List<Amendment_Activity__c> amendmentActivityList = new List<Amendment_Activity__c>();
                                
                                //Now Copy all the Shareholder Records associated to the Account...
                                for(Account_Activity__c accountActivityObject : amendmentAccountActivityList) 
                                {
                                    Amendment_Activity__c amendmentActivityObject = new Amendment_Activity__c();
                                    amendmentActivityObject.Amendment__c = amendmentObject.Id;
                                    amendmentActivityObject.Account__c = accountActivityObject.Account__c;
                                    amendmentActivityObject.Activity__c = accountActivityObject.Activity__c;
                                    amendmentActivityObject.Active__c = true;
                                    amendmentActivityObject.Account_Activity__c = accountActivityObject.id;
                                    //Adding in the list...
                                    amendmentActivityList.add(amendmentActivityObject);
                                }
                                
                                //Now Insert the Amendment Activity Records...
                                if(amendmentActivityList.size() > 0)
                                    insert amendmentActivityList;
                                
                                System.debug('The Amendment Object Id =>'+ amendmentObject.id);
                                PageReference pageRef = new PageReference('/'+amendmentObject.id);
                                return pageRef;
                                
                            }
                            else
                            {
                                ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, 'Please select the New License Type to Continue !!!');
                                ApexPages.addMessage(msg);
                                return null;
                            }
                        
                        }
                        else
                        {
                            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, 'Expired Tenancy Contract(s) is available. Please perform Tenancy Contract Renewal before proceeding');
                            ApexPages.addMessage(msg);
                            return null;
                        }
                    
                    }
                    //Shareholder Amendment - Add Partner Business Logic...
                    else if(amendmentRecordTypeMap.get(selectedRecordType).Name  == 'Shareholder Amendment - Add Partner' || amendmentRecordTypeMap.get(selectedRecordType).Name == 'Shareholder Amendment - Change Share Percentage' || amendmentRecordTypeMap.get(selectedRecordType).Name == 'Shareholder Amendment - Remove Partner' || amendmentRecordTypeMap.get(selectedRecordType).Name == 'Shareholder Amendment - Replace Partner')
                    {
                        if(amendmentTenancyContractForKeyMap.size() >= 0 )
                        {
                            List<Shareholder_Amendment__c> shareHolderAmendmentList = new List<Shareholder_Amendment__c>();
                            
                            //For Account Object...
                            accountObject.recordtypeId = accountRecordType.id;
                            accountObject.License_Application_Type__c = 'Amendment';
                            
                            //For Creating the Amendment Record...
                            Amendment__c amendmentObject = new Amendment__c();
                            amendmentObject.recordTypeId = amendmentRecordTypeMap.get(selectedRecordType).id;
                            amendmentObject.Related_To__c = 'Share Ownership';
                            amendmentObject.Account__c = ApexPages.CurrentPage().getParameters().get('accountId');
                            
                            //Here is the Business Logic...
                            if(amendmentRecordTypeMap.get(selectedRecordType).Name == 'Shareholder Amendment - Change Share Percentage')
                            {
                                if(accountObject.LegalStatus__r.Short_Name_English__c != 'F.Z.C')
                                {
                                    ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, 'You cannot perform Change Share Percentage Amendment for non F.Z.C Company.');
                                    ApexPages.addMessage(msg);
                                    return null; 
                                
                                }
                                
                                accountObject.License_Application_Step__c = 'Change Share Percentage Amendment Started';
                                amendmentObject.Amendment_Application_Status__c = 'Change Share Percentage Amendment Initiated';
                                amendmentObject.Action__c = 'Edit';
                            }
                            else if(amendmentRecordTypeMap.get(selectedRecordType).Name == 'Shareholder Amendment - Add Partner')
                            {
                                
                                accountObject.License_Application_Step__c = 'Add Partner Amendment Started';
                                amendmentObject.Amendment_Application_Status__c = 'Add Partner Amendment Initiated';
                                amendmentObject.Action__c = 'Add';
                                
                                       
                            }
                            else if(amendmentRecordTypeMap.get(selectedRecordType).Name == 'Shareholder Amendment - Remove Partner')
                            {
                                
                                accountObject.License_Application_Step__c = 'Remove Partner Amendment Started';
                                amendmentObject.Amendment_Application_Status__c = 'Remove Partner Amendment Initiated';
                                amendmentObject.Action__c = 'Delete';
                                
                                       
                            }
                            else if(amendmentRecordTypeMap.get(selectedRecordType).Name == 'Shareholder Amendment - Replace Partner')
                            {
                                
                                accountObject.License_Application_Step__c = 'Replace Partner Amendment Started';
                                amendmentObject.Amendment_Application_Status__c = 'Replace Partner Amendment Initiated';
                                amendmentObject.Action__c = 'Edit';
                                
                                       
                            }
                            //Now Update the Account Object...
                            update accountObject;
                            
                            System.debug('After Updating the Account');                                
                            //Now Insert the Amendment Object...
                            insert amendmentObject;
                            
                            //Now Copy all the Shareholder Records associated to the Account...
                            for(Share_Ownership__c shareOwnershipObject : shareOwnerShipList) 
                            {
                                Shareholder_Amendment__c shareAmendmentObject = new Shareholder_Amendment__c();
                                shareAmendmentObject.Amendment__c = amendmentObject.Id;
                                shareAmendmentObject.Contact__c = shareOwnershipObject.Shareholder__c;
                                shareAmendmentObject.Account__c = shareOwnershipObject.Company__c;
                                shareAmendmentObject.Ownership__c = shareOwnershipObject.Ownership__c;
                                shareAmendmentObject.Share_Profit__c = shareOwnershipObject.Share_Profit__c;
                                shareAmendmentObject.Share_Ownership__c = shareOwnershipObject.Id;
                            	
                            	shareAmendmentObject.Previous_Ownership__c = shareOwnershipObject.Ownership__c;
                            	shareAmendmentObject.Previous_Shareholder__c = shareOwnershipObject.Shareholder__c;
                            	shareAmendmentObject.Previous_ShareProfit__c =shareOwnershipObject.Share_Profit__c;    
                                //Adding in the list...
                                shareHolderAmendmentList.add(shareAmendmentObject);
                            }
                            
                            //Now Insert the ShareOwnership AmendmentRecords...
                            if(shareHolderAmendmentList.size() > 0)
                                insert shareHolderAmendmentList;
                            
                            System.debug('The Amendment Object Id =>'+ amendmentObject.id);
                            PageReference pageRef = new PageReference('/'+amendmentObject.id);
                            return pageRef;
                            
                        
                        }
                        else
                        {
                            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, 'Expired Tenancy Contract(s) is available. Please perform Tenancy Contract Renewal before proceeding');
                            ApexPages.addMessage(msg);
                            return null;
                        
                        }
                    
                    }
                    //Company Amendment - Change Company Manager Business Logic....
                    else if(amendmentRecordTypeMap.get(selectedRecordType).Name  == 'Company Amendment - Change Company Manager')
                    {
                        if(amendmentTenancyContractForKeyMap.size() >= 0 )
                        {
                                accountObject.recordtypeId = accountRecordType.id;
                                accountObject.License_Application_Type__c = 'Amendment';
                                accountObject.License_Application_Step__c = 'Change Company Manager Amendment Started';
                                update accountObject;
                                
                                System.debug('After Updating the Account');
                                //For Creating the Amendment Record...
                                Amendment__c amendmentObject = new Amendment__c();
                                amendmentObject.recordTypeId = amendmentRecordTypeMap.get(selectedRecordType).id;
                                amendmentObject.Amendment_Application_Status__c = 'Change Company Manager Amendment Initiated';
                                amendmentObject.Related_To__c = 'Company';
                                amendmentObject.Account__c = ApexPages.CurrentPage().getParameters().get('accountId');
                                
                                //Previous Company Manager...
                                amendmentObject.Previous_Authorized_Manager__c = accountObject.Authorized_Manager__c;
                                
                                //Now Insert the Amendment Object...
                                insert amendmentObject;
                                
                                System.debug('The Amendment Object Id =>'+ amendmentObject.id);
                                PageReference pageRef = new PageReference('/'+amendmentObject.id);
                                return pageRef;
                        }
                        else
                        {
                            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, 'Expired Tenancy Contract(s) is available. Please perform Tenancy Contract Renewal before proceeding');
                            ApexPages.addMessage(msg);
                            return null;
                        }
                    
                    }
                    //Company Amendment - Change Company Name Business Logic...
                    else if(amendmentRecordTypeMap.get(selectedRecordType).Name  == 'Company Amendment - Change Company Name')
                    {
                        if(amendmentTenancyContractForKeyMap.size() >= 0 )
                        {
                                accountObject.recordtypeId = accountRecordType.id;
                                accountObject.License_Application_Type__c = 'Amendment';
                                accountObject.License_Application_Step__c = 'Change Company Name Amendment Started';
                                update accountObject;
                                
                                System.debug('After Updating the Account');
                                //For Creating the Amendment Record...
                                Amendment__c amendmentObject = new Amendment__c();
                                amendmentObject.recordTypeId = amendmentRecordTypeMap.get(selectedRecordType).id;
                                amendmentObject.Amendment_Application_Status__c = 'Change Company Name Amendment Initiated';
                                amendmentObject.Related_To__c = 'Company';
                                amendmentObject.Account__c = ApexPages.CurrentPage().getParameters().get('accountId');
                                
                                //Previous Company Names...
                                amendmentObject.Previous_Company_Name__c = accountObject.Name;
                                amendmentObject.Previous_Company_Name_Arabic__c = accountObject.company_name_arabic__c;
                                
                                //Now Insert the Amendment Object...
                                insert amendmentObject;
                                
                                System.debug('The Amendment Object Id =>'+ amendmentObject.id);
                                PageReference pageRef = new PageReference('/'+amendmentObject.id);
                                return pageRef;
                        }
                        else
                        {
                            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, 'Expired Tenancy Contract(s) is available. Please perform Tenancy Contract Renewal before proceeding');
                            ApexPages.addMessage(msg);
                            return null;
                        }
                                           
                    }
                    //For Location Amendment - Add Location Business Logic...
                    else if(amendmentRecordTypeMap.get(selectedRecordType).Name == 'Location Amendment - Add Location')
                    {
                        //For Tenancy Contract Check...
                        if(amendmentTenancyContractForKeyMap.size() >= 0)
                        {
                            Account selectedAccountObject =(Account)controllerProperty.getRecord();
                            //Account accountObject = new Account();
                            //accountObject = [select id,RecordTypeId,License_Application_Step__c,Amendment_Change_Location__c,Amendment_Change_Location_2__c,Amendment_Change_Location_3__c,Residence_Visa_Quota_Left_2__c from Account where id =: ApexPages.CurrentPage().getParameters().get('accountId') ];
                            System.debug('The Selected New Location => '+selectedAccountObject.Amendment_Change_Location__c);//selectedAccountObject.Amendment_Change_Location__c
                            if(selectedAccountObject.Amendment_Change_Location__c != null || selectedAccountObject.Amendment_Change_Location_2__c != null || selectedAccountObject.Amendment_Change_Location_3__c != null)
                            {
                                //List<String> locationIDList = new List<String>(string.valueof(selectedAccountObject.Amendment_Change_Location__c) ,string.valueof(selectedAccountObject.Amendment_Change_Location_2__c),string.valueof(selectedAccountObject.Amendment_Change_Location_3__c));
                                List<String> locationIDList = new List<String>();
                                locationIDList.add(selectedAccountObject.Amendment_Change_Location__c);
                                locationIDList.add(selectedAccountObject.Amendment_Change_Location_2__c);
                                locationIDList.add(selectedAccountObject.Amendment_Change_Location_3__c);
                                List<Location__c> tempNewLocationObjectList = new List<Location__c>([select Id,Name,Location_Booked_Information_Flag__c,Visa_Quota__c from Location__c where Id IN: (locationIDList)]);
                                
                                //RecordType accountRecordType = new RecordType();
                                //accountRecordType = [select Id,SObjectType,IsActive,Name,DeveloperName from RecordType where IsActive =: true AND SObjectType =: 'Account' AND DeveloperName =: 'Under_Amendment'];
                                
                                accountObject.recordtypeId = accountRecordType.id;
                                accountObject.License_Application_Type__c = 'Amendment';
                                accountObject.License_Application_Step__c = 'Add Location Amendment Started';
                                update accountObject;
                                
                                List<Location__c> locationUpdateList = new List<Location__c>();
                                
                                for(Location__c tempNewLocation :tempNewLocationObjectList )
                                {
                                    if(tempNewLocation.Visa_Quota__c == null)
                                        tempNewLocation.Visa_Quota__c = 0;
                                    System.debug('The tem New Location Visa => '+ tempNewLocation.Id);
                                    System.debug('The tem New Location Visa => '+ tempNewLocation.Visa_Quota__c);
                                    System.debug('The Account Selected Visa => '+accountObject.Residence_Visa_Quota_Left_2__c);
                                    
                                    if((accountObject.Residence_Visa_Quota_Left_2__c + tempNewLocation.Visa_Quota__c) > 0)
                                    {
                                        //Now Update the Locations...
                                        tempNewLocation.Location_Booked_Information_Flag__c = true;
                                        
                                        //Now Add the Location in Update List...
                                        locationUpdateList.add(tempNewLocation);
                                                            
                                    
                                    }
                                    else
                                    {
                                        ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, 'New Location Selection ('+tempNewLocation.Name +')doesnot have enough visa Quota, Please select another Location');
                                        ApexPages.addMessage(msg);
                                        return null;
                                    }
                                    
                                    
                                }
                                System.debug('The LOcation Update Size => '+locationUpdateList.size() );
                                //For Updating the Locations...
                                update locationUpdateList;
                                //For Creating the Amendment Record...
                                Amendment__c amendmentObject = new Amendment__c();
                                amendmentObject.recordTypeId = amendmentRecordTypeMap.get(selectedRecordType).id;
                                amendmentObject.Amendment_Application_Status__c = 'Add Location Amendment Initiated';
                                amendmentObject.Related_To__c = 'Location';
                                //Add Location...
                                amendmentObject.New_Location__c = selectedAccountObject.Amendment_Change_Location__c;
                                amendmentObject.New_Location_2__c = selectedAccountObject.Amendment_Change_Location_2__c;
                                amendmentObject.New_Location_3__c = selectedAccountObject.Amendment_Change_Location_3__c;
                                amendmentObject.Account__c = ApexPages.CurrentPage().getParameters().get('accountId');
                                
                                //Now Insert the Amendment Object...
                                insert amendmentObject;
                                
                                System.debug('The Amendment Object Id =>'+ amendmentObject.id);
                                PageReference pageRef = new PageReference('/'+amendmentObject.id);
                                return pageRef;
                                
                            }
                            else
                            {
                                ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, 'Please select one location to Add');
                                ApexPages.addMessage(msg);
                                return null;   
                            } 
                        }
                        else
                        {
                            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, 'Expired Tenancy Contract(s) is available. Please perform Tenancy Contract Renewal before proceeding');
                            ApexPages.addMessage(msg);
                            return null;
                        }
                    }
                    //For Location Amendment - Remove Location Business Logic...
                    else if(amendmentRecordTypeMap.get(selectedRecordType).Name == 'Location Amendment - Remove Location')
                    {
                        //For Tenancy Contract Check...
                        if(amendmentTenancyContractForKeyMap.size() >= 0)
                        {
                            List<Tenancy_Contract__c> selectedTenancyContractList = new List<Tenancy_Contract__c>();
                                
                            //First Check the Locations Selected or Not...
                            for(wrapperClass WCP : removeLocationItemList)
                            {
                                if(WCP.removeLocationChecked == true)
                                    selectedTenancyContractList.add(WCP.wrapTenancyContract);
                                    
                            } 
                            System.debug('The Number Of Selected Remove Locations are => ' + selectedTenancyContractList.size());
                            if(selectedTenancyContractList.size() > 0)
                            {
                                if(selectedTenancyContractList.size() <= 3)
                                {
                                    System.debug('The Location Size  => ' + (removeLocationItemList.size() - selectedTenancyContractList.size() ));
                                    //First Check the Validation that One Location should be associate to the Company...
                                    if((removeLocationItemList.size() - selectedTenancyContractList.size() ) < 1)
                                    {
                                        ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, 'You have reached the maximum number of Activities to remove');
                                        ApexPages.addMessage(msg);
                                        return null;
                                    }
                                    System.debug('In Account Creation => ' + selectedTenancyContractList.size());
                                    //Account accountObject = new Account();
                                    //accountObject = [select id, Activity__c,RecordTypeId,License_Application_Step__c from Account where Id =: ApexPages.CurrentPage().getParameters().get('accountId')];
                                        
                                    //Now Update the Account and Insert the Amendment Object...
                                    if(accountObject != null)
                                    {
                                        accountObject.recordTypeId = accountRecordType.Id;
                                        accountObject.License_Application_Type__c = 'Amendment';
                                        accountObject.License_Application_Step__c = 'Remove Location Amendment Started';
                                        System.debug('Before Account Update');
                                        System.debug('The Account Object => '+ accountObject.License_Application_Step__c);
                                        update accountObject;
                                        
                                        //For Creating the Amendment Record...
                                        Amendment__c amendmentObject = new Amendment__c();
                                        amendmentObject.recordTypeId = amendmentRecordTypeMap.get(selectedRecordType).id;
                                        amendmentObject.Amendment_Application_Status__c = 'Remove Location Amendment Initiated';
                                        amendmentObject.Related_To__c = 'Location';
                                        amendmentObject.Account__c = ApexPages.CurrentPage().getParameters().get('accountId');
                                        
                                        for(integer i=0; i<selectedTenancyContractList.size(); i++ )
                                        {
                                            if(i == 0)
                                            {
                                                amendmentObject.New_Location__c = selectedTenancyContractList[i].Location__c;
                                                amendmentObject.Previous_Tenancy_Contract__c =  selectedTenancyContractList[i].id;
                                            }
                                            else if(i == 1)
                                            {     
                                                amendmentObject.New_Location_2__c = selectedTenancyContractList[i].Location__c;
                                                amendmentObject.Previous_Tenancy_Contract_2__c =  selectedTenancyContractList[i].id;
                                            }
                                            else if(i == 2)
                                            {
                                                amendmentObject.New_Location_3__c = selectedTenancyContractList[i].Location__c; 
                                                amendmentObject.Previous_Tenancy_Contract_3__c =  selectedTenancyContractList[i].id;
                                            }   
                                        }    
                                        
                                        //Now Insert the Amendment Object...
                                        insert amendmentObject;
                                        System.debug('After Amendment Creation => ' + selectedTenancyContractList.size());
                                        //Redirect user to different page...
                                        PageReference pageRef = new PageReference('/'+amendmentObject.id);
                                        return pageRef;
                                    }
                                }
                                else
                                {
                                    System.debug('Only Remove 3 Location at a time => ' + selectedTenancyContractList.size());
                                    ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, 'You can Remove only 3 Locations at once !!!');
                                    ApexPages.addMessage(msg);
                                    return null;
                                } 
                                
                            
                            }
                            else
                            {
                                ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, 'Please select atleast one Location to initiate the process!!!');
                                ApexPages.addMessage(msg);
                                return null;
                            }                                
                                
                            
                        }
                        else
                        {
                            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, 'Expired Tenancy Contract(s) is available. Please perform Tenancy Contract Renewal before proceeding');
                            ApexPages.addMessage(msg);
                            return null;
                        }
                    }
                    
                    //For Change Company Activity Business Logic...
                    else if(amendmentRecordTypeMap.get(selectedRecordType).Name == 'Company Amendment - Change Company Activity')
                    {
                        if(selectedChangeLocationOperations != null )
                        {
                            //For Tenancy Contract Check...
                            if(amendmentTenancyContractForKeyMap.size() >= 0)
                            {
                                
                                List<Account_Activity__c> selectedAccountActivityList = new List<Account_Activity__c>();
                                
                                //First Check the Activity Selected or Not...
                                for(wrapperClass WCP : accountActivityItemList)
                                {
                                    if(WCP.accountActivityChecked == true)
                                        selectedAccountActivityList.add(WCP.wrapAccountActivity);
                                }    
                                
                                if(selectedAccountActivityList.size() <= 3)
                                {
                                    //Account accountObject = new Account();
                                    //accountObject = [select id, Activity__c,RecordTypeId,License_Application_Step__c from Account where Id =: ApexPages.CurrentPage().getParameters().get('accountId')];
                                    System.debug('The Query Account License Application Step ==> '+ accountObject.License_Application_Step__c);
                                    System.debug('The Query Account Activity ==> '+ accountObject.Activity__c);
                                    
                                    if(selectedChangeLocationOperations == 'Add')
                                    {
                                    	//List<Account_Activity__c> accountActivityList = new List<Account_Activity__c>([select id,Active__c from Account_Activity__c where Active__c =: true AND Account__c =: accountObject.Id]);
                                        //Check the validations For Account Activities Add...
                                        //if((accountObject.Activity__c == 'Trading - One Activity' && selectedAccountActivityList.size() >= 1) || (accountObject.Activity__c == 'Trading - Two Activity' && selectedAccountActivityList.size() >= 2) ||(accountObject.Activity__c == 'Trading - Three Activity' && selectedAccountActivityList.size() >= 3))
                                        //if((accountObject.Activity__c == 'Trading - One Activity' && accountActivityList.size() >= 1) || (accountObject.Activity__c == 'Trading - Two Activity' && accountActivityList.size() >= 2) ||(accountObject.Activity__c == 'Trading - Three Activity' && accountActivityList.size() >= 3))
                                        if((accountObject.Activity__c == 'Trading - One Activity' && accountObject.Active_Account_Activities__c >= 1) || (accountObject.Activity__c == 'Trading - Two Activity' && accountObject.Active_Account_Activities__c >= 2) ||(accountObject.Activity__c == 'Trading - Three Activity' && accountObject.Active_Account_Activities__c >= 3))
                                        {
                                            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, 'You have reached the maximum number of Activities for this License Type');
                                            ApexPages.addMessage(msg);
                                            return null;
                                        }                       
                                    }
                                    else if(selectedChangeLocationOperations == 'Delete' || selectedChangeLocationOperations == 'Edit')
                                    {
                                        
                                        //Check the User selected anything in activities....
                                        if(selectedAccountActivityList.size() < 1)
                                        {
                                        	ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, 'Please select the activities to continue the process...');
                                            ApexPages.addMessage(msg);
                                            return null;
                                        	
                                        }
                                        if(selectedChangeLocationOperations == 'Delete')
                                        {
                                        	//First Check the Validation that One Activities should be associate to the Company...
	                                        if((accountActivityItemList.size() - selectedAccountActivityList.size() ) < 1)
	                                        {
	                                            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, 'You have reached the maximum number of Activities to remove');
	                                            ApexPages.addMessage(msg);
	                                            return null;
	                                        }	
                                        }
                                        
                                        
                                    }
                                    //Now Update the Account and Insert the Amendment Object...
                                    if(accountObject != null)
                                    {
                                        System.debug('The Query Account License Application Step ==> '+ accountObject.License_Application_Step__c);
                                        System.debug('The Query Account Activity ==> '+ accountObject.Activity__c);
                                    
                                        //Change the Account Record Type TO Amendment...
                                        //RecordType accountRecordType = new RecordType();
                                        //accountRecordType = [select Id,SObjectType,IsActive,Name,DeveloperName from RecordType where IsActive =: true AND SObjectType =: 'Account' AND DeveloperName =: 'Under_Amendment'];
                                        
                                        accountObject.recordTypeId = accountRecordType.Id;
                                        accountObject.License_Application_Type__c = 'Amendment';
                                        accountObject.License_Application_Step__c = 'Change Company Activity Amendment Started';
                                        System.debug('Before Account Update');
                                        System.debug('The Account Object => '+ accountObject.License_Application_Step__c);
                                        update accountObject;
                                        
                                        //For Creating the Amendment Record...
                                        Amendment__c amendmentObject = new Amendment__c();
                                        amendmentObject.recordTypeId = amendmentRecordTypeMap.get(selectedRecordType).id;
                                        amendmentObject.Amendment_Application_Status__c = 'Change Company Activity Initiated';
                                        amendmentObject.Related_To__c = 'Company';
                                        amendmentObject.Account__c = ApexPages.CurrentPage().getParameters().get('accountId');
                                        amendmentObject.Action__c = 'Add';
                                        
                                        //For Edit and Delete Operations...
                                        if(selectedChangeLocationOperations == 'Edit' || selectedChangeLocationOperations == 'Delete')
                                        {
                                            if(selectedChangeLocationOperations == 'Edit')
                                                amendmentObject.Action__c = 'Edit';
                                            else if(selectedChangeLocationOperations == 'Delete')   
                                                 amendmentObject.Action__c = 'Delete';
                                                 
                                            for(integer i=0; i<selectedAccountActivityList.size(); i++ )
                                            {
                                                if(i == 0)
                                                    amendmentObject.Current_Activity_1__c = selectedAccountActivityList[i].Activity__c;
                                                else if(i == 1)     
                                                    amendmentObject.Current_Activity_2__c = selectedAccountActivityList[i].Activity__c;
                                                else if(i == 2)
                                                    amendmentObject.Current_Activity_3__c = selectedAccountActivityList[i].Activity__c; 
                                            }
                                        }
                                        
                                        //Now Insert the Amendment Object...
                                        insert amendmentObject;
                                        
                                        //Redirect user to different page...
                                        PageReference pageRef = new PageReference('/'+amendmentObject.id);
                                        return pageRef;
                                        
                                    }   
                                    
                                    
                                }
                                else
                                {
                                    ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, 'You can Amend only 3 activities at once !!!');
                                    ApexPages.addMessage(msg);
                                    return null;
                                }       
                            }
                            else
                            {
                                ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, 'Expired Tenancy Contract(s) is available. Please perform Tenancy Contract Renewal before proceeding');
                                ApexPages.addMessage(msg);
                                return null;
                            }
                            
                                
                    }
                    else
                    {
                        
                        ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, 'Please select the Account Activity first !!!');
                        ApexPages.addMessage(msg);
                        return null;
                    }
                    
                }
                
                //For Location Amendment - Change Location Business Logic...
                else if(amendmentRecordTypeMap.get(selectedRecordType).Name == 'Location Amendment - Change Location')
                {
                    System.debug('The Location Change Selected => '+ selectedLocationChange);
                    if(selectedLocationChange != null )//|| selectedLocationChange != '')
                    {
                        //For Tenancy Contract Check...
                        if(amendmentTenancyContractForKeyMap.size() >= 0)
                        {
                            Account selectedAccountObject =(Account)controllerProperty.getRecord();
                            //Account accountObject = new Account();
                            //accountObject = [select id,RecordTypeId,License_Application_Step__c,Residence_Visa_Quota_Left_2__c from Account where id =: ApexPages.CurrentPage().getParameters().get('accountId') ];
                            System.debug('The Selected New Location => '+selectedAccountObject.Amendment_Change_Location__c);//selectedAccountObject.Amendment_Change_Location__c
                            
                            if(selectedAccountObject.Amendment_Change_Location__c != null)
                            {
                                
                                Location__c tempNewLocation = new Location__c();
                                tempNewLocation = [select Id,Location_Booked_Information_Flag__c,Visa_Quota__c from Location__c where Id=: selectedAccountObject.Amendment_Change_Location__c];
                                
                                if(tempNewLocation.Visa_Quota__c == null)
                                    tempNewLocation.Visa_Quota__c = 0;
                                
                                if(amendmentTenancyContractForKeyMap.get(selectedLocationChange).Location__r.Visa_Quota__c == null)
                                    amendmentTenancyContractForKeyMap.get(selectedLocationChange).Location__r.Visa_Quota__c = 0;
                                    
                                System.debug('The tem New Location Visa => '+ tempNewLocation.Id);
                                System.debug('The tem New Location Visa => '+ tempNewLocation.Visa_Quota__c);
                                System.debug('The Previous Selected Location Visa => '+ amendmentTenancyContractForKeyMap.get(selectedLocationChange).Location__r.Visa_Quota__c);
                                System.debug('The Account Selected Visa => '+accountObject.Residence_Visa_Quota_Left_2__c);
                                //if(((accountObject.Residence_Visa_Quota_Left_2__c + tempNewLocation.Visa_Quota__c) - (amendmentTenancyContractForKeyMap.get(selectedLocationChange).Location__r.Visa_Quota__c)) > 0)
                                if(((accountObject.Residence_Visa_Quota_Left_2__c + tempNewLocation.Visa_Quota__c) - (amendmentTenancyContractForKeyMap.get(selectedLocationChange).Location__r.Visa_Quota__c)) >= 0)
                                {
                                    //RecordType accountRecordType = new RecordType();
                                    //accountRecordType = [select Id,SObjectType,IsActive,Name,DeveloperName from RecordType where IsActive =: true AND SObjectType =: 'Account' AND DeveloperName =: 'Under_Amendment'];
                                    accountObject.recordtypeId = accountRecordType.id;
                                    accountObject.License_Application_Type__c = 'Amendment';
                                    accountObject.License_Application_Step__c = 'Change Location Amendment Started';
                                    update accountObject;
                                    
                                    //Now Update the Locations...
                                    tempNewLocation.Location_Booked_Information_Flag__c = true;
                                    update tempNewLocation;
                                    
                                    System.debug('After Updating the Account');
                                    //For Creating the Amendment Record...
                                    Amendment__c amendmentObject = new Amendment__c();
                                    amendmentObject.recordTypeId = amendmentRecordTypeMap.get(selectedRecordType).id;
                                    amendmentObject.Amendment_Application_Status__c = 'Change Location Amendment Initiated';
                                    amendmentObject.Related_To__c = 'Location';
                                    //Change Location...
                                    amendmentObject.Location_ForKey__c = amendmentTenancyContractForKeyMap.get(selectedLocationChange).Location__r.Id;
                                    //Changed Location...
                                    System.debug('The Previous Location => '+ amendmentTenancyContractForKeyMap.get(selectedLocationChange).Location__r.Id + ' && The Current New Location => '+ selectedAccountObject.Amendment_Change_Location__c);
                                    amendmentObject.New_Location__c = selectedAccountObject.Amendment_Change_Location__c;
                                    amendmentObject.Account__c = ApexPages.CurrentPage().getParameters().get('accountId');
                                    amendmentObject.Previous_Tenancy_Contract__c = amendmentTenancyContractForKeyMap.get(selectedLocationChange).id;
                                    //Now Insert the Amendment Object...
                                    insert amendmentObject;
                                    
                                    System.debug('The Amendment Object Id =>'+ amendmentObject.id);
                                    PageReference pageRef = new PageReference('/'+amendmentObject.id);
                                    return pageRef;
                                    
                                }   
                                else
                                {
                                    ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, 'New Location Selection doesnot have enough visa Quota, Please select another Location');
                                    ApexPages.addMessage(msg);
                                    return null;    
                                    
                                }
                            }
                            else
                            {
                                ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, 'Please select the New Location');
                                ApexPages.addMessage(msg);
                                return null;    
                                
                            }
                            
                        }
                        else
                        {
                            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, 'Expired Tenancy Contract(s) is available. Please perform Tenancy Contract Renewal before proceeding');
                            ApexPages.addMessage(msg);
                            return null;
                            
                        }
                        
                        
                        
                    }
                    else
                    {
                        
                        ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, 'Please select the Location first !!!');
                        ApexPages.addMessage(msg);
                        return null;
                    }
                    
                }
                
                //For Location New Key Business Logic...
                else if(amendmentRecordTypeMap.get(selectedRecordType).Name == 'Location Amendment - New Key')
                {
                    
                    //Selected Locations...
                    if(selectedLocationKey != null)
                    {
                        //For Tenancy Contract Check...
                        if(amendmentTenancyContractForKeyMap.size() >= 0)
                        {
                        
                            //Account account = new Account();
                            //account = [select id,License_Application_Step__c, RecordTypeId from Account where Id =: ApexPages.CurrentPage().getParameters().get('accountId')];
                            
                            //RecordType accountRecordType = new RecordType();
                            //accountRecordType = [select Id,SObjectType,IsActive,Name,DeveloperName from RecordType where IsActive =: true AND SObjectType =: 'Account' AND DeveloperName =: 'Under_Amendment'];
                            
                            //Now Updating the Account Object...
                            accountObject.License_Application_Type__c = 'Amendment';
                            accountObject.License_Application_Step__c = 'New Key Amendment Started';
                            accountObject.RecordTypeId = accountRecordType.id;
                            
                            update accountObject;
                            
                            //For Creating the Amendment Record...
                            Amendment__c amendmentObject = new Amendment__c();
                            amendmentObject.recordTypeId = amendmentRecordTypeMap.get(selectedRecordType).id;
                            if(amendmentTenancyContractForKeyMap.get(selectedLocationKey).Location__r.Number_of_Keys__c != null || amendmentTenancyContractForKeyMap.get(selectedLocationKey).Location__r.Number_of_Keys__c != '')
                                amendmentObject.Current_of_Keys__c = integer.valueOf(amendmentTenancyContractForKeyMap.get(selectedLocationKey).Location__r.Number_of_Keys__c);
                            amendmentObject.Amendment_Application_Status__c = 'New Key Process Initiated';
                            amendmentObject.Related_To__c = 'Location';
                            amendmentObject.Location_ForKey__c = amendmentTenancyContractForKeyMap.get(selectedLocationKey).Location__r.Id;
                            amendmentObject.Account__c = ApexPages.CurrentPage().getParameters().get('accountId');
                            //Now Insert the Amendment Object...
                            insert amendmentObject;
                            
                            PageReference pageRef = new PageReference('/'+amendmentObject.id);
                            return pageRef;
                            
                        }
                        else
                        {
                            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, 'Expired Tenancy Contract(s) is available. Please perform Tenancy Contract Renewal before proceeding');
                            ApexPages.addMessage(msg);
                            return null;
                            
                        }   
                    }
                    else
                    {
                        
                        ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, 'Please select the Location first !!!');
                        ApexPages.addMessage(msg);
                        return null;
                    }
                    
                }
            
            }
            else
            {
                ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, 'Please select the Options for the Amendments !!!');
                ApexPages.addMessage(msg);
                return null;
                
            }
                
        }
        catch(Exception exp)
        {
            System.debug('The Save Button => '+ exp.getMessage());
            return null;
        }
        
        //Return the value...
        return null;
        
        
    }
    
    //For Cancel Button Method...
    public PageReference cancelPageMethod()
    {
        Pagereference pageRef = new PageReference('/'+ApexPages.CurrentPage().getParameters().get('accountId'));
        return pageRef;
        
    }
    
    
    //Wrapper Class...
    public class wrapperClass
    {
        public Account_Activity__c wrapAccountActivity{get;set;}
        public Boolean accountActivityChecked{get;set;}
        public Boolean removeLocationChecked {get;set;}
        public Tenancy_Contract__c wrapTenancyContract{get;set;}
       
        //For Change Account Activity...
        public wrapperClass(Account_Activity__c wrapActivityItem)
        {
            wrapAccountActivity = wrapActivityItem;
            accountActivityChecked = false;
        }
        
        //For Remove Location...
        public wrapperClass(Tenancy_Contract__c wrapTenancyContractItem)
        {
            wrapTenancyContract = wrapTenancyContractItem;
            removeLocationChecked = false;
        }
    }
    
    
    
}